<?php
// +----------------------------------------------------------------------
// |ManageModel.class.php
// +----------------------------------------------------------------------
// |Data:2015-01-08 22:04
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;
use Common\Model\CommonModel;

class ManageModel extends CommonModel{
    protected $tableName = 'manage';

    /**
     * 列表
     * @param \Common\Model\表名 $limit
     * @return mixed
     */
    public function lists($limit){
        $map = array();
        $map['m.id'] = array('neq',$_SESSION[C('USER_AUTH_KEY')]);
        $map['m.id'] = array('neq',1);
        $list = M()->table(C('DB_PREFIX')."auth_group as g")
            ->join('LEFT JOIN '.C('DB_PREFIX').'manage as m on m.type = g.id')
            ->field('m.*,g.title')
            ->where($map)
            ->order('m.add_time desc')
            ->cache(5)
            ->findPage($limit);
        return $list;
    }

    /**
     * 获取客服手机号
     */
    public function getManageMobile($type){
        if(empty($type)) return false;
        if(S('manage_mobile_list_'.$type)){
            $list = S('manage_mobile_list_'.$type);
        }else{
            $where = array();
            $where['type'] = $type;
            $where['status'] = 1;
            $list = $this->where($where)->field('mobile')->select();
            S('manage_mobile_list_'.$type,$list);
        }
        $str = '';
        if($list){
            foreach($list as $val){
                if($val){
                    $str .= $val['mobile'].',';
                }
            }
            $str = trim($str,',');
        }

        return $str;
    }

    /**
     * 详情
     * @param \Common\Model\表名 $id
     * @return mixed
     */
    public function detail($id){
        $info = M()
            ->table(C('DB_PREFIX')."manage m")
            ->join(C('DB_PREFIX')."auth_group_access as g on g.uid = m.id")
            ->field("m.*,g.group_id")
            ->where('id='.$id)
            ->cache(5)
            ->find();
        return $info;
    }

    public function info($id){
        return parent::detail($id);
    }

    /**
     * 分组列表
     * @return mixed
     */
    public function group_list(){
        return M('auth_group')->where(array('status'=>1))->cache(5)->select();
    }


    /**
     * 更新角色
     * @param $data
     * @return bool
     */
    public function updateGroupAccess($data,$id = null){
        if($id){
            return M('auth_group_access')->where(array('uid'=>$data['uid']))->setField('group_id',$data['group_id']);
        }else{
            return M('auth_group_access')->add($data);
        }
    }

    /**
     * 删除权限里的用户
     * @param $where
     * @return bool
     */
    public function delGroupByUid($where){
        return M('auth_group_access')->where($where)->delete();
    }

    /**
     * 获取管理员名称
     * @param $id
     * @return bool|string
     */
    public function getManageNickname($id){
        if(empty($id)) return false;
        return $this->where(array('id'=>$id))->getField('nickname');
    }

    /**
     * 数据更新
     * @param $data
     * @return bool
     */
    public function updateManage($data){
        if(empty($data) || !is_array($data)) return false;
        if($data['id']){
            return $this->save($data);
        }else{
            return $this->add($data);
        }
    }

    /**
     * 获取管理列表
     * @param $map
     * @return bool|mixed
     */
    public function getManageList($map){
        if(empty($map)) return false;
        $list = M('manage')->where($map)->field('id,account,nickname')->select();
        return $list;
    }
}
?>